package com.study.d012Components;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.geometry.Orientation;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import javafx.util.StringConverter;

/**
 * 滑动条
 * @author Administrator
 *
 */
public class SliderMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}@Override
	public void start(Stage stage) throws Exception {
		stage.show();

		AnchorPane anchorPane = new AnchorPane();
		Slider slider = new Slider(0, 100, 20);
		//设置值
		slider.setValue(20);

		//设置方向
		slider.setOrientation(Orientation.VERTICAL);

		//在滑动条上显示刻度
		slider.setShowTickLabels(true);

		//显示更详细的刻度
		slider.setShowTickMarks(true);
		//设置主要刻度间隔
		slider.setMajorTickUnit(10);

		//slider.setSnapToTicks(true);

		anchorPane.getChildren().add(slider);
		AnchorPane.setTopAnchor(slider, 20.0);
		AnchorPane.setLeftAnchor(slider, 20.0);

		stage.setScene(new Scene(anchorPane,400,300));

		//格式化标签
		slider.setLabelFormatter(new StringConverter<Double>() {
			@Override
			public String toString(Double object) {
				if(object==0){
					return "零";
				}else if(object ==100){
					return "一百";
				}
				return "未知";
			}

			@Override
			public Double fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});

		//滑块监听事件
		/*slider.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				System.out.println("当前滑动的值是:" + newValue);
			}
		});*/


		//设置自动滑动( 笨方法)
		/*new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < 100; i++) {
					slider.setValue(i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();*/


		//通过JavaFX的多任务进行更新
		MyScheduledService my = new MyScheduledService(slider);
		my.setDelay(Duration.millis(0));
		my.setPeriod(Duration.millis(1000));
		my.start();



		//另外还可以通过ScheduledService.valueProperty()来实现滑块自动滑动， 这样只需要在多任务中对变量进行递增即可，该方法可以监听到值的变化
		my.valueProperty().addListener(new ChangeListener<Number>() {
			@Override
			public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
				System.out.println(oldValue + "---" + newValue);
				if(newValue !=null){
					if(newValue.intValue() ==100){
						my.cancel();
					}else{
						slider.setValue(newValue.intValue());
					}
				}
			}
		});

	}
}


/**
 * javaFx多任务  多线程
 * @author Administrator
 *
 */
class MyScheduledService extends ScheduledService<Integer>{
	Slider s;
	int i=0;

	public MyScheduledService(Slider s){
		this.s = s;
	}

	@Override
	protected Task<Integer> createTask() {
		Task<Integer> task = new Task<Integer>() {
			@Override
			protected Integer call() throws Exception {
				return i++;
			}

			/**
			 * 通过updateValue()方法更新组件
			 */
			/*@Override
			protected void updateValue(Integer value) {
				System.out.println(value);
				super.updateValue(value);
				System.out.println(i);
				s.setValue(i);
				if(i==100){
					//取消任务
					MyScheduledService.this.cancel();
				}
			}*/
		};

		return task;
	}
}
